<?php
/**
 * Smarty plugin
 * -------------------------------------------------------------
 * Type:     block
 * Name:     pmvc_missingFormErrors
 * Purpose:  renders the block only if there are not any errors
 * -------------------------------------------------------------
 */

use pmvc\template\smarty\SmartyUtil;
use pmvc\util\ReflectionUtil;

function smarty_block_pmvc_missingFormErrors($params, $content, &$smarty, &$repeat) {
	
	// get errors
	$errors = isset($params["errors"])
		? $params["errors"] 
		: SmartyUtil::getTemplateVar($smarty, "formErrors");

	// no errors
    if (empty($errors) || count($errors)==0) {
        return $content;
    }

	// if it's the object, get the error array
	if (ReflectionUtil::isInstance($errors, 'pmvc\mvc\controller\FormErrors')) {
		$errors = $errors->get();
	}

	// make sure that they're not empty
	$hasErrors = false;
	if (array_key_exists("globalErrors", $errors)
		&& count($errors["globalErrors"])>0) {
		$hasErrors = true;
	} else if (array_key_exists("fieldErrors", $errors)) {
		foreach ($errors["fieldErrors"] as $fieldError) {
			if (count($fieldError)>0) {
				$hasErrors = true;
				break;
			}
		}
	}

	return ($hasErrors) ? '' : $content;
}

?>